<?php

class AdminController extends Controller
{
	public $layout = 'awt-reference';
	
	public function actionIndex()
	{
		$this->render('index');
	}
	
	public function actionSearch() {
		
		$referenceSearcher = new ReferenceSearcher();
		
		Yii::import('ext.zsearcher.Zsearcher');
		$searcher = new Zsearcher($referenceSearcher);
		$searcher->normalSearch();

		$this->render('search',array(
			'result'	=>$searcher->getResult(),
			'pager'		=>$searcher->getPager(),
			'filter'	=>$searcher->getFilter(),
		));

	}
	
	public function actionQuickview($id) {
		
		$this->layout = 'awt-quickview';
		
		$rs = Reference::model()->with('album')->findByPk($id);
		
		$this->render('quickview',array(
			'rs'	=>$rs
		));
	}
	
	public function actionCreate() {
		
		$model = new Reference();
		
		if ( !empty($_POST) && isset($_POST['Reference']) ) {
			
			$model->attributes = $_POST['Reference'];
			if ($model->save()) {
				
				Yii::app()->user->setFlash('success',"Data saved!");
			} else {
				echo "<pre>"; print_r( $model->getErrors() ); exit;
				Yii::app()->user->setFlash('error',"There is an Error");
			}
			$this->refresh();
		}
		
		$this->render('create',array(
			'model'=>$model,
		));
	}
	
	public function actionUpdate($id) {
		
		$model = new Reference('update');
		$model = $model->findByPk($id);

		if ( !empty($_POST) && isset($_POST['Reference']) ) {
			
			$model->attributes = $_POST['Reference'];
			if ($model->save()) {
				Yii::app()->user->setFlash('success',"Data saved!");
			} else {
				echo "<pre>"; print_r( $model->getErrors() ); exit;
			}
			$this->refresh();
		}
		$this->render('create',array(
			'model'=>$model,
		));
	}
	
	public function actionDelete() {
		
		if ( !empty($_POST) && isset($_POST['Reference']) ) {
			$rs = Reference::model()->deleteByPk($_POST['Reference']['id']);
			if ($rs) {
				Yii::app()->user->setFlash('success',"Reference Deleted");
			} else {
				echo "<pre>"; print_r( $rs->getErrors() ); exit;
				Yii::app()->user->setFlash('error',"There is an Error");
			}
		}
		$this->redirect(Yii::app()->request->getUrlReferrer());
	}
}